<?php
/**
 * Created by PhpStorm.
 * User: cgp
 * Date: 14-8-23
 * Time: 下午4:34
 * 获取关联产品（套票）
 * url:http://www.12301.cc/module/link_product/index.php?token=密钥&appid=base64_encode(来源域名)&pid=产品ID
 */
define('ROOT_PATH','/var/www/html/new/');
//校验
$token = trim($_GET['token']);
$appId = base64_decode(trim($_GET['appid']));
$pid   = (int)$_GET['pid'];
$tokens = include 'config.php';
include ROOT_PATH . '/com.inc.php';
include ROOT_PATH . '/d/class/SoapInit.class.php';
include ROOT_PATH . '/d/class/Sale.class.php';
include ROOT_PATH . '/d/class/ProductInfo.php';

if (!isset($tokens[$appId]) || $token != $tokens[$appId]) {
    exit('{"status":0,"msg":"Access Denied!"}');
}

if ($_GET['signature'] != md5($token.$_GET['appid'].$pid)) {
    exit('{"status":0,"msg":"Access Denied2!"}');
}


if (!$pid || !is_numeric($pid)) {
    exit('{"status":0,"msg":"Empty pid"}');
}
$saleObject = new \pft\Prod\Sale($GLOBALS['le'], $_SESSION['sid']);
$soap = SoapInit::GetSoapInside();
$date = date('Y-m-d');
//print_r($products);
//exit;
//TODO:套票产品查看子票
if (isset($_GET['showTicket'])) {
    $sql = "SELECT l.attribute,l.id,l.apply_did FROM uu_land l LEFT JOIN uu_jq_ticket t ON t.landid=l.id WHERE t.pid=$pid";
    $GLOBALS['le']->query($sql);
    $row = $GLOBALS['le']->fetch_assoc();
    $includeTickets = json_decode($row['attribute']);
    $child_pid_file = $includeTickets[0]->pid;
    $aid =  $row['apply_did'];
    $my_product = include dirname(__FILE__) . "/data/p_{$child_pid_file}_{$row['id']}.inc.php";
    //print_r($my_product);exit;
    //TODO::套票验证
    $beginTime = trim($_REQUEST['beginTime']);
    $tnum      = intval($_REQUEST['tnum']);
    $aid       = intval($_REQUEST['aid']);
    $chk_ret = \pft\Prod\Sale::PackageTicketCheck($soap, $pid,
        (int)$_SESSION['sid'], $aid, $beginTime, $tnum);
    $child_invalid_msg = array();
    if ($chk_ret['code']!==200) {
        if ($chk_ret['code']>=909) {
            $child_invalid_msg[$chk_ret['pid']] = $chk_ret['msg'];
        }
        else {
            echo json_encode($chk_ret);
            exit;
        }
    }
    $msg = '';
    $m   = '';
    if (isset($child_invalid_msg[$child_pid_file])) {
        $msg = '<p class="pt-invalid-msg">'.$child_invalid_msg[$child_pid_file] .'</p>';
        $m = $child_invalid_msg[$child_pid_file];
    }
    $tickets[] = array(
            'pid'       => $child_pid_file,
            'l_title'   => $my_product['l_title'],
            't_title'   => $my_product['t_title'],
            'l_img'     => $my_product['l_img'],
            'num'       => $my_product['num'],
            'msg'       => $msg,
            'm'         => $m,
        );
    foreach ($my_product['link_ids'] as $key=>$prod) {
        $c_msg = '';
        $c_m   = '';
        if (isset($child_invalid_msg[$key])) {
            $c_msg = '<p class="pt-invalid-msg">'.$child_invalid_msg[$key] .'</p>';
            $c_m = $child_invalid_msg[$key];
        }
        $tickets[] = array(
            'pid'       => $key,
            'l_title'   => $prod['l_title'],
            't_title'   => $prod['t_title'],
            'l_img'     => $prod['l_img'],
            'num'       => $prod['num'],
            'msg'       => $c_msg,
            'm'         => $c_m,
        );
    }
    echo json_encode(array('code'=>$chk_ret['code'],'list'=>$tickets));
    exit;
}

$products = array();
$output   = array();
foreach (glob( dirname(__FILE__) . "/data/p_{$pid}_*.inc.php", GLOB_BRACE) as $filename) {
    $output[] = include $filename;
}
foreach ($output as $prod) {
    $products[$pid][] = $prod;
}
if (!isset($products[$pid])) {
    exit('{"status":0,"msg":"Product not exist"}');
}
if (! $_SESSION['memberID']) {
    exit('{"status":0,"msg":"Login Timeout Or Unlogin!"}');
}

//判断购买权限begin


//$sale->iCanSalePid($packId);
//exit;
$product       = $products[$pid];
$packetProduct = array();
$prices = array();
$pros   = array();
//print_r($product);
$output = array();
$members = include dirname(__FILE__) . '/Member.cache.php';
$Y_m_d = date('Y-m-d');
foreach($product as $key=>$p) {
    $sales   = $saleObject->salePros($p['lid']);//查询会员可分销产品信息
    foreach ($sales as $sale) {
        if ($sale['pid']!=$p['pid']) continue;
        $p['aid'] = $sale['sapply_did'];
        $p['aid_name'] = $members[$sale['sapply_did']];
        //5为普通用户
        //从接口获取零售价
        $xml = $soap->Dynamic_Price_And_Storage($_SESSION['saccount'],$sale['pid'],$Y_m_d,1,1,0,$sale['sapply_did']);
//        echo $xml;
        if($xml==105){
            $xml = $soap->Dynamic_Price_And_Storage($_SESSION['saccount'],$sale['pid'],$Y_m_d,2,1,0,$sale['sapply_did']);
        }
        if ($xml==105) {
            continue;
        }
        else{
            $prices['uprice'] = (int)simplexml_load_string($xml)->Rec->UUsprice;
        }

        if ($_SESSION['dtype']!=5) {
            $xml = $soap->Dynamic_Price_And_Storage($_SESSION['saccount'],$sale['pid'],$Y_m_d,1,0,0,$sale['sapply_did']);
            if($xml==105){
                $xml = $soap->Dynamic_Price_And_Storage($_SESSION['saccount'],$sale['pid'],$Y_m_d,2,0,0,$sale['sapply_did']);
            }
            if ($xml==105) {
                continue;
            }
            else{
                $prices['jsprice'] = (int)simplexml_load_string($xml)->Rec->UUsprice;
            }
        }
        else {
            //散客身份，结算价等于零售价
//            $jsprice = $uprice;
            $prices['jsprice'] = $prices['uprice'];
            //$prices['uprice'] = $uprice;
        }
//        $prices = array(
//            "jsprice"=> $jsprice,
//            "uprice" => $uprice,
//        );
        $packetProduct[$p['lid']] = array(
            'price'    => $prices,
            'product'  => $p,
        );
    }

//    if ( !$sale->iCanSale($packId) ) {
//        continue;
//    }
//    $pro     = new ProductInfo($soap,$packId);
//    $jsprice = $pro->jsPrice($_SESSION['memberID'],$date);
//    $uprice  = $pro->uPrice($_SESSION['memberID'],$date);
//    if ($_SESSION['dtype']==5) {//普通用户
//        $jsprice = $uprice;
//    }
//    $storage = $pro->dayStorage($date);
//    $prices = array(
//        "jsprice"=>$jsprice,
//        "uprice"=>$uprice,
//        "storage"=>$storage
//    );
//    $packetProduct[$lid] = array(
//        'price'    => $prices,
//        'product'  => $p,
//    );
}
//print_r($packetProduct);
//exit;
if (count($packetProduct)) {
    echo json_encode($packetProduct);
}
else {
    echo '{"status":0}';
}
